package com.zhejiangzhengyuan.municipal_way.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.RoadGis;
import com.zhejiangzhengyuan.municipal_common.entity.dto.FileDto;
import com.zhejiangzhengyuan.municipal_common.entity.vo.RoadVo;
import com.zhejiangzhengyuan.municipal_common.mapper.FileMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.RoadGisMapper;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.BIService;
import com.zhejiangzhengyuan.municipal_common.service.InspectionTasksService;
import com.zhejiangzhengyuan.municipal_common.service.RoadGisService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import com.zhejiangzhengyuan.municipal_common.utils.excelFileUtils.ExcelResolveListener;
import com.zhejiangzhengyuan.municipal_common.utils.excelFileUtils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/** @author lin_ju Create on 2019/11/28 */
@Api(tags = "道路相关信息")
@RestController
@RequestMapping("/way/road")
@Slf4j
public class RoadGisController {

  @Autowired private RoadGisService roadGisService;
  @Autowired private InspectionTasksService inspectionTasksService;
  @Autowired private RoadGisMapper roadGisMapper;
  @Autowired private FileMapper fileMapper;
  @Autowired private BIService biService;

  @ApiOperation("查询道路并分页")
  @GetMapping("/findPage")
  public ResponseVo findPage(@ApiParam("页码") Long page, @ApiParam("每页显示条数") Long limit) {
    // pageNo默认为1；size默认为15
    page = page == null || page <= 0 ? 1 : page;
    limit = limit == null || limit <= 0 ? 15 : limit;
    IPage<RoadGis> pageGis = roadGisService.page(page, limit);
    return pageGis == null
        ? new ResponseVo<>(ConstantUtil.ResultCode.SERVER_ERROR, "没有数据")
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, pageGis);
  }

  @ApiOperation("根据ID删除")
  @DeleteMapping("/removeRoadById/{id}")
  public ResponseVo removeRoadById(@PathVariable Integer id) {
    return roadGisService.removeById(id) ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("根据ID 批量删除 格式：'ids':[7,9,10]")
  @DeleteMapping("/removeRoadByIds")
  public ResponseVo removeRoadByIds(@RequestBody Map<String, List<Integer>> map) {
    List<Integer> idList = map.get("ids");
    return roadGisService.removeByIds(idList) ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("根据ID更新")
  @PutMapping("/updateRoad")
  public ResponseVo updateRoad(@RequestBody  RoadGis roadGis) {
    return roadGisService.updateRoad(roadGis) > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("添加道路")
  @PostMapping("/insertRoad")
  public ResponseVo saveRoad(
      @ApiParam(name = "添加桥梁字段", required = true) @RequestBody @Validated RoadGis roadGis) {
    return roadGisService.saveRoad(roadGis) > 0
        ? new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, roadGis)
        : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("根据坐标查询桥梁 请求格式Map<startPoint,111,222>")
  @GetMapping("/findPointByRoad")
  public ResponseVo findPointByRoad(String startPoint, String endPoint) {
    RoadGis roadGis = roadGisService.findPointByRoad(startPoint, endPoint);
    return Objects.isNull(roadGis)
        ? ResponseVo.CLIENT_PARAM_ERROR
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, roadGis);
  }

  @ApiOperation("导出数据")
  @GetMapping("/export")
  public void excelRoad(HttpServletResponse response) {
    QueryWrapper<RoadGis> wrapper = new QueryWrapper<RoadGis>().orderByAsc("id");
    List<RoadGis> list = roadGisService.list(wrapper);
    HashSet<String> exclude = new HashSet<>();
    exclude.add("createTime");
    exclude.add("updateTime");
    ExcelUtil.objectToExcelAndDownload(response, exclude, "道路数据表", RoadGis.class, list);
  }

  @ApiOperation("更新道路文件")
  @PostMapping("/updateRoadFile")
  public ResponseVo updateRoadFile(
          @ModelAttribute FileDto dto) {
    int i = roadGisService.updateRoadFile(dto);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.CLIENT_PARAM_ERROR;
  }
//  @ApiOperation("根据ID查询图片地址")
//  @GetMapping("/selectLinkByRoadId")
//  public ResponseVo selectLinkByRoadId(String code){
//    QueryWrapper<RoadGis> roadGisQueryWrapper = new QueryWrapper<>();
//    roadGisQueryWrapper.eq("code",code);
//    RoadGis roadGis = roadGisMapper.selectOne(roadGisQueryWrapper);
//    List<String> linkList = fileMapper.selectByRid(roadGis.getId());
//    return linkList==null?
//            ResponseVo.SERVER_ERROR:
//            new ResponseVo(ConstantUtil.ResultCode.SUCCESS, linkList);
//  }

  @ApiOperation("查询桥梁信息并分页")
  @GetMapping("/selectAllRoad")
  public ResponseVo selectAllRoad(@ApiParam("页码") Long page, @ApiParam("每页显示条数") Long limit) {
    page = Objects.isNull(page) || page <= 0 ? 1 : page;
    limit = Objects.isNull(limit) || limit <= 0 ? 14 : limit;
    Page<RoadVo> roadVoPage = new Page<>(page, limit);
    IPage<RoadVo> roadVoIPage = roadGisService.selectAllRoad(roadVoPage);
    return Objects.isNull(roadVoIPage)
        ? ResponseVo.SERVER_ERROR
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, roadVoIPage);
  }

  @ApiOperation("导入道路")
  @RequestMapping(value = "/importRoad", method = RequestMethod.POST)
  public ResponseVo importBridge(@RequestParam("file") MultipartFile file) {
    return ExcelUtil.readExcel(file, RoadGis.class, new ExcelResolveListener<>(roadGisService));
  }

  @ApiOperation("手机端道路专题，数据显示")
  @GetMapping(value = "/mobileTerminalRoadDisplay")
  public ResponseVo mobileTerminalRoadDisplay(Integer month) {
    String type = "道路";
    return ResponseVo.successWithData(biService.getBICount(type, month,null));
  }

  @ApiOperation("道路模糊查询")
  @RequestMapping(value = "/FuzzyRoad", method = RequestMethod.GET)
  public ResponseVo FuzzyRoad(
      String roleName, String code, @ApiParam("页码") Long page, @ApiParam("每页显示条数") Long limit) {
    page = Objects.isNull(page) || page <= 0 ? 1 : page;
    limit = Objects.isNull(limit) || limit <= 0 ? 14 : limit;
    Page<RoadVo> bridgeVoPage = new Page<>(page, limit);
    Page<RoadVo> road = roadGisMapper.FuzzyRoad(bridgeVoPage, roleName, code);
    List<RoadVo> records = road.getRecords();
    for (RoadVo roadVo : records) {
      List<String> strings = fileMapper.selectByBid(roadVo.getId());
      if (strings==null||strings.size()==0) {
        roadVo.setLink(strings);
      }
    }
    return ResponseVo.successWithData(road);
  }
}
